import random
from Solution import Solution
from typing import List

def countOf4Sum(nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
    hashmap = dict()
    for n1 in nums1:
        for n2 in nums2:
            hashmap[n1+n2] = hashmap.get(n1+n2, 0) + 1
    
    # 如果 -(n1+n2) 存在于nums3和nums4, 存入结果
    count = 0
    for n3 in nums3:
        for n4 in nums4:
            key = - n3 - n4
            if key in hashmap:
                count += hashmap[key]
    return count

def generateNumArrays() -> List[List[int]]:
    arrayLength = random.randrange(100, 200)
    numArrays = [[0] * arrayLength for _ in range(4)]
    for n in range(0, arrayLength):
        sum = 0
        nums = list()
        for i in range(4):
            num = random.randrange(-268435456, 268435456)
            if i == 3:
                select = random.choice([True, False])
                if select:
                    nums.append(-sum)
                    break
            nums.append(num)
            sum += num
        
        random.shuffle(nums)

        for i in range(4):
            numArrays[i][n] = nums[i]
    return numArrays

def checkSolution(arrays = None):
    if arrays == None:
        arrays = generateNumArrays()
    a = s.answer(*arrays)
    e = countOf4Sum(*arrays)
    if a != e:
        print("Test Failed\nInput:", arrays, "Expected:", e, "Got:", a)
        exit(1)

s = Solution()

checkSolution([[0], [0], [0], [0]])
checkSolution([[1,2], [-2,-1], [-1,2], [0,2]])

for i in range(50):
    checkSolution()    

exit(0)
